package com.heima.article.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticleService;
import com.heima.common.cache.CacheService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.HotArticleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class HotArticleServiceImpl implements HotArticleService {
    //todo 回来看
    @Autowired
    private CacheService cacheService;
    @Autowired
    private ApArticleMapper apArticleMapper;
    @Override
    public void computHotArticle() {
        //1.查询前五天发布的文章
        LocalDateTime last5Day = LocalDateTime.now().minusDays(5);
        List<ApArticle> allLsit=apArticleMapper.findArticleByLast5Days(last5Day);
        if (CollectionUtils.isEmpty(allLsit)) {
            log.info("最新5天没有新文章");
        }
        //2.计算集合中所有文章的总热度
        List<HotArticleVo> hotResultList = new ArrayList<>();
        for (ApArticle apArticle : allLsit) {
            if(apArticle==null){
                continue;
            }
            //2.1计算当前文章热度
            HotArticleVo vo = computeHot(apArticle);
            //2.2添加热度
            hotResultList.add(vo);
        }
        //3.分组(频道)
        List<List<HotArticleVo>> sortResultLists = CollectionUtil.groupByField(hotResultList, "channelId");
        if(CollectionUtils.isEmpty(sortResultLists)){
            log.warn("分组数据为空");
            return;
        }
        //4.遍历分组
        for (List<HotArticleVo> hotArticleVos : sortResultLists) {
            if(CollectionUtils.isEmpty(hotArticleVos)){
                continue;
            }
            Integer channelId = hotArticleVos.get(0).getChannelId();
            sortAndCaching(hotArticleVos,channelId.toString());
        }
        //5.热门排序
        sortAndCaching(hotResultList,ArticleConstants.DEFAULT_TAG);

    }
    //计算当前文章热度
    private HotArticleVo computeHot(ApArticle apArticle){

        Integer score=0;
        Integer views = apArticle.getViews();
        Integer likes = apArticle.getLikes();
        Integer comment = apArticle.getComment();
        Integer collection = apArticle.getCollection();
        //阅读 1分
        if(views!=null){
            score+=views*1;
        }
        //点赞 3分
        if(likes!=null){
            score+=likes*ArticleConstants.HOT_ARTICLE_LIKE_WEIGHT;//用静态变量效果更好
        }
        //评论 5分
        if(comment!=null){
            score+=comment*5;
        }
        //收藏 8分
        if(collection!=null){
            score+=collection*8;
        }

        HotArticleVo vo = new HotArticleVo();
        vo.setScore(score);
        BeanUtils.copyProperties(apArticle,vo);
        return vo;
    }
    //遍历分组,排序和写入
    private void sortAndCaching(List<HotArticleVo> voList,String channelId){
        //1.排序 stream.sortd
        voList= voList.stream().sorted(new Comparator<HotArticleVo>() {
            @Override
            public int compare(HotArticleVo o1, HotArticleVo o2) {
                return (Integer) (o1.getScore() - o2.getScore());
            }//o1-o2从小到大(正序),o2-o1从大到小(倒叙)
        }).collect(Collectors.toList());
//        List<HotArticleVo> collect = voList.stream().sorted(Comparator.comparing(HotArticleVo::getScore).reversed())
//                .collect(Collectors.toList());
        //2.截取前30条
        if (voList.size()>30) {
            voList=voList.subList(0,30);
        }
        //3.写入到redis
        cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE+channelId, JSON.toJSONString(voList));
    }
}
